import path from "node:path";
import { fileURLToPath, pathToFileURL, URL } from 'node:url'
import fs from 'node:fs'
import { defineConfig, loadEnv } from "vite";
import vue from '@vitejs/plugin-vue'
import VueJsx from "@vitejs/plugin-vue-jsx";
// import electronPlugin from 'vite-plugin-electron-renderer';
// import optimizer from 'vite-plugin-optimizer'

import { ENV_PATH, outBaseDir, PUBLIC_DIR } from '../nodetools/project';
import { wrapperEnv } from '../nodetools/vite.util';

const outDir = path.join(outBaseDir, 'index');



export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, ENV_PATH, "");
  const viteEnv = wrapperEnv(env);
  const { VITE_RENTER_ADMIN_PORT } = viteEnv;
  return {
    define: {
      __APP_ENV__: env.APP_ENV,
    },
    plugins: [
      vue(),
      VueJsx(),
    ],
    base: "./",
    root: ".",
    envPrefix: "VITE_",
    envDir: ENV_PATH,
    publicDir: PUBLIC_DIR,
    server: {
      host: "127.0.0.1",
      port: VITE_RENTER_ADMIN_PORT,
    },
    build: {
      target: "modules",
      outDir: outDir,
      assetsDir: "assets",
      sourcemap: false,
      emptyOutDir: true,
      copyPublicDir: true,
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url)),
      }
    },
    
    logLevel: "info",
  }
})

